package com.ea.admin.controller.tutelage;

import com.ea.admin.pojo.param.report.AnalyzeVideoParam;
import com.ea.admin.pojo.param.report.GetReportDetailParam;
import com.ea.admin.pojo.param.report.GetReportListByUserIdParam;
import com.ea.admin.service.PDFGenerationService;
import com.ea.admin.service.ReportService;
import com.ea.admin.utils.NowUserGetter;
import com.ea.common.auth.NowUser;
import com.ea.common.log.LogAnnotation;
import com.ea.common.report.pojo.vo.ReportDetailInfoVo;
import com.ea.common.report.pojo.vo.ReportSimpleInfoListVo;
import com.ea.common.result.BizException;
import com.ea.common.result.ErrorCode;
import com.ea.common.result.Result;
import com.ea.common.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@RestController
@Api(tags = "监护管理-报告管理")
@RequestMapping("/admin/report")
public class ReportController {
    @Resource
    ReportService reportService;
    @Resource
    NowUserGetter nowUserGetter;
    @Resource
    PDFGenerationService pdfGenerationService;

    @Transactional
    @Operation(summary = "获得指定人员的所有报告")
    @PostMapping("/getReportListByAccountId")
    @ApiImplicitParam(dataType = "GetReportListByUserIdParam", name = "getReportListByUserIdParam", value = "获得报告", readOnly = true, required = true)
    @LogAnnotation(module = "ReportController", operation = "获得人员报告")
    public Result<ReportSimpleInfoListVo> getReportListByAccountId(@RequestBody @Valid GetReportListByUserIdParam getReportListByUserIdParam) {
        NowUser nowUser = nowUserGetter.getNowUser();
        if (nowUser == null) {
            throw new BizException(ErrorCode.USER_LOGIN_ERROR_NO_LOGIN);
        }
        ReportSimpleInfoListVo result = reportService.getReportListByAccountId(
                nowUser.getAccountId(),
                ParamUtil.parseId(getReportListByUserIdParam.getUserId()),
                getReportListByUserIdParam.getWarningTypeList(),
                getReportListByUserIdParam.getStatusList(),
                getReportListByUserIdParam.getName(),
                getReportListByUserIdParam.getPostStartDate(),
                getReportListByUserIdParam.getPostEndDate(),
                getReportListByUserIdParam.getReceiveStartDate(),
                getReportListByUserIdParam.getReceiveEndDate(),
                getReportListByUserIdParam.getEmoAnalyStartDate(),
                getReportListByUserIdParam.getEmoAnalyEndDate(),
                getReportListByUserIdParam.getHeartRateStartDate(),
                getReportListByUserIdParam.getHeartRateEndDate(),
                getReportListByUserIdParam.getPage(),
                getReportListByUserIdParam.getPageSize()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "分析已上传的视频")
    @PostMapping("/analyzeVideo")
    @ApiImplicitParam(dataType = "AnalyzeVideoParam", name = "analyzeVideoParam", value = "分析已上传的视频", readOnly = true, required = true)
    @LogAnnotation(module = "ReportController", operation = "分析已上传的视频")
    public Result analyzeVideo(@RequestBody @Valid AnalyzeVideoParam analyzeVideoParam) {
        NowUser nowUser = nowUserGetter.getNowUser();
        if (nowUser == null) {
            throw new BizException(ErrorCode.USER_LOGIN_ERROR_NO_LOGIN);
        }
        reportService.analyzeVideo(
                nowUser.getAccountId(),
                ParamUtil.parseId(analyzeVideoParam.getUserId()),
                analyzeVideoParam.getMd5(),
                // 上传视频 type = 0
                0);
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "获取报告详情")
    @PostMapping("/getReportDetail")
    @ApiImplicitParam(dataType = "GetReportDetailParam", name = "getReportDetailParam", value = "获取报告详情", readOnly = true, required = true)
    @LogAnnotation(module = "ReportController", operation = "获取报告详情")
    public Result<ReportDetailInfoVo> getReportDetail(@RequestBody @Valid GetReportDetailParam getReportDetailParam) {
        NowUser nowUser = nowUserGetter.getNowUser();
        if (nowUser == null) {
            throw new BizException(ErrorCode.USER_LOGIN_ERROR_NO_LOGIN);
        }
        ReportDetailInfoVo result = reportService.getReportDetail(
                nowUser.getAccountId(),
                ParamUtil.parseId(getReportDetailParam.getReportId()));
        return Result.success(result);
    }

//    @Transactional
//    @Operation(summary = "获取报告内容")
//    @PostMapping("/getReportInfo")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long"),
//            @ApiImplicitParam(name = "md5", value = "报告MD5", required = true, dataType = "String")
//    })    @LogAnnotation(module = "ReportController", operation = "获取报告详情")
//    public Result<ReportDetailInfoVo> getReportInfo(@RequestParam String userId,
//                                                      @RequestParam String md5) {
//        ReportDetailInfoVo result = reportService.getReportInfo(userId,md5);
//        return Result.success(result);
//    }

    /**
     * 下载PDF报告
     * @param response HTTP响应对象
     */
    @GetMapping("/getReportPDF")
    @LogAnnotation(module = "ReportController", operation = "获得报告PDF")
    public void getReportPDF(
            @RequestBody @Valid GetReportDetailParam getReportDetailParam,
            HttpServletResponse response
    ) {
        NowUser nowUser = nowUserGetter.getNowUser();
        if (nowUser == null) {
            throw new BizException(ErrorCode.USER_LOGIN_ERROR_NO_LOGIN);
        }
        pdfGenerationService.generateAndOutputPDF(
                nowUser.getAccountId(),
                ParamUtil.parseId(getReportDetailParam.getReportId()),
                response
        );
    }

}
